<?php
define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');
/* 检查权限 */
admin_priv('gc_user_search');

if($_REQUEST['act'] == 'list')
{
    $list = search_list();
    $smarty->assign('full_page',        1);
    $smarty->assign('user_search',      $list['item']);
    $smarty->assign('filter',           $list['filter']);
    $smarty->assign('record_count',     $list['record_count']);
    $smarty->assign('page_count',       $list['page_count']);
    $smarty->assign('ur_here',          '客户搜索记录');

    assign_query_info();
    $smarty->display('gc_user_search.htm');
}

elseif ($_REQUEST['act'] == 'query')
{
    $list = search_list();
    $smarty->assign('user_search',    $list['item']);
    $smarty->assign('filter',         $list['filter']);
    $smarty->assign('record_count',   $list['record_count']);
    $smarty->assign('page_count',     $list['page_count']);

    $sort_flag  = sort_flag($list['filter']);
    $smarty->assign($sort_flag['tag'], $sort_flag['img']);

    make_json_result($smarty->fetch('gc_user_search.htm'), '', array('filter' => $list['filter'], 'page_count' => $list['page_count']));
}

elseif ($_REQUEST['act'] == 'batch_drop')
{
    if (isset($_POST['checkboxes']))
    {
        $del_count = 0; //初始化删除数量
        foreach ($_POST['checkboxes'] AS $key => $id)
        {
            /* 取得搜索信息 */
            $user_search = user_search_info($id);
                /* 删除查询 */
                $sql = "DELETE FROM " . $GLOBALS['ecs']->table('user_search') . " WHERE id = '$id' LIMIT 1";
                $GLOBALS['db']->query($sql, 'SILENT');
                /*记录管理员操作信息*/
                admin_log(addslashes($user_search['keywords']) . '[' . $id . ']', 'remove', 'user_search');
                $del_count++;
        }

        /* 如果删除了历史搜索，清除缓存 */
        if ($del_count > 0)
        {
            clear_cache_files();
        }

        $links[] = array('text' => $_LANG['back_list'], 'href'=>'gc_user_search.php?act=list');
        sys_msg(sprintf('删除成功', $del_count), 0, $links);
    }
    else
    {
        $links[] = array('text' => $_LANG['back_list'], 'href'=>'gc_user_search.php?act=list');
        sys_msg('删除失败', 0, $links);
    }
}
/**
 * 取得历史搜索信息
 * @param   int     $user_search_id   历史搜索id
 * @return  array
 */
function user_search_info($user_search_id)
{
    /* 取得搜索信息 */
    $user_search_id = intval($user_search_id);
    $sql = "SELECT *, id AS user_search_id " .
        "FROM " . $GLOBALS['ecs']->table('user_search') .
        " WHERE id = '$user_search_id' ";
    $user_search = $GLOBALS['db']->getRow($sql);
    return $user_search;
}
/*
 * 取得历史搜索列表
 * @return   array
 */
function search_list()
{
    $result = get_filter();
    if ($result === false)
    {
        /* 过滤条件 */
        $filter['keyword']      = empty($_REQUEST['keyword']) ? '' : trim($_REQUEST['keyword']);
        if (isset($_REQUEST['is_ajax']) && $_REQUEST['is_ajax'] == 1)
        {
            $filter['keyword']  = json_str_iconv($filter['keyword']);
        }
        $filter['sort_by']      = empty($_REQUEST['sort_by']) ? 'id' : trim($_REQUEST['sort_by']);
        $filter['sort_order']   = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);
        /*搜索提交条件*/
        $where = (!empty($filter['keyword'])) ? " AND ( keywords LIKE '%" . mysql_like_quote($filter['keyword'])."%' OR chinese LIKE '%".mysql_like_quote($filter['keyword']). "%' ) " : '';
        /*查询语句*/
        $sql = "SELECT COUNT(*) FROM " . $GLOBALS['ecs']->table('user_search') . " WHERE 1 = 1 $where";

        $filter['record_count'] = $GLOBALS['db']->getOne($sql);
        /* 分页大小 */
        $filter = page_and_size($filter);
        /* 查询 */
        $sql = "SELECT * ". "FROM " . $GLOBALS['ecs']->table('user_search') . " WHERE 1 = 1 $where ".
            " ORDER BY " . $filter['sort_by'] . " " . $filter['sort_order'] .
            " LIMIT ". $filter['start'] .", " . $filter['page_size'];

        $filter['keyword'] = stripslashes($filter['keyword']);
        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }

    $list = $GLOBALS['db']->getAll($sql);
    $arr = array('item' => $list, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
    return $arr;
}

?>